//
//  MainViewController.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/12.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import UIKit
import SnapKit

enum ControllerStatus {
    case Full
    case Small
}

class MainViewController: UIViewController {

    //MARK: --------------------------- Life Cycle --------------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        initRootViewController()
        
        
    }
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return statusBarStyle
    }
    override func prefersStatusBarHidden() -> Bool {
        return statusBarHidden
    }
    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
        return .Slide
    }
    
    //MARK: --------------------------- Getter and Setter --------------------------
    // 底部菜单
    private let menuController = MenuViewController()
    // 首页
    private let homeViewController = HomeViewController()
    // 快讯
    private let newsFlashController = NewsFlashViewController()
    // Appso
    private let appSoController = AppSoViewController()
    // 玩物志
    private let playzhiController = PlayingZhiViewController()
    // MindStore
    private let mindStoreController = MindStoreViewController()
    
    private var viewArray = [UIView]()
    private var coverBtnArray = [UIButton]()
    
    // 缩放值
    private let scale: CGFloat = 0.4
    // 状态栏相关
    private var statusBarStyle: UIStatusBarStyle = .LightContent
    private var statusBarHidden: Bool = true
    
    // 默认是填充状态
    private var vcState = ControllerStatus.Full
    
    //MARK: --------------------------- UI --------------------------
    private lazy var scrollView: UIScrollView = {
        var scrollView = UIScrollView(frame: CGRect(x: 0, y: UIConstant.UI_MARGIN_20, width: UIConstant.SCREEN_WIDTH, height: UIConstant.SCREEN_HEIGHT-UIConstant.UI_MARGIN_20))
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        return scrollView
    }()
    
    /// 顶部标题
    private lazy var headerView: MainHeaderView = {
        var headerView: MainHeaderView = MainHeaderView(frame: CGRect(x: 0, y: 0, width: 5*UIConstant.SCREEN_WIDTH, height: 20))
        return headerView
    }()
    
    /// 菜单按钮
    private lazy var menuBtn : UIButton = {
        let menuBtn = UIButton()
        menuBtn.setImage(UIImage(imageLiteral:"ic_hamburg"), forState: .Normal)
        menuBtn.addTarget(self, action: #selector(MainViewController.menuBtnDidClick(_:)), forControlEvents: .TouchUpInside)
        return menuBtn
    }()
    
    /// 顶部红线
    private lazy var redLine: UIView = {
        let redLine = UIView()
        redLine.frame = CGRect(x: self.view.center.x-20, y: 0, width: 40, height: 1)
        redLine.backgroundColor = UIConstant.UI_COLOR_RedTheme
        return redLine
    }()
    
    private func setupUI() {
        self.view.backgroundColor = UIColor.redColor()
        self.view.addSubview(menuController.view)
        
        self.view.addSubview(scrollView)
        self.view.addSubview(headerView)
        self.view.addSubview(menuBtn)
        self.view.addSubview(redLine)
        self.menuBtn.snp_makeConstraints { (make) in
            make.right.equalTo(-15)
            make.top.equalTo(35)
            make.width.height.equalTo(45)
        }
    }
    
    private func initRootViewController() {
        newsFlashController.scrollViewReusableDataSource = self
        homeViewController.scrollViewReusableDataSource = self
        playzhiController.scrollViewReusableDataSource = self
        appSoController.scrollViewReusableDataSource = self
        mindStoreController.scrollViewReusableDataSource = self
        
        newsFlashController.scrollViewReusableDelegate = self
        homeViewController.scrollViewReusableDelegate = self
        playzhiController.scrollViewReusableDelegate = self
        appSoController.scrollViewReusableDelegate = self
        mindStoreController.scrollViewReusableDelegate = self
        
        menuController.view.frame = self.view.bounds
        self.addChildViewController(menuController)
        self.addChildViewController(newsFlashController)
        self.addChildViewController(homeViewController)
        self.addChildViewController(playzhiController)
        self.addChildViewController(appSoController)
        self.addChildViewController(mindStoreController)
        
        viewArray.append(newsFlashController.view)
        viewArray.append(homeViewController.view)
        viewArray.append(playzhiController.view)
        viewArray.append(appSoController.view)
        viewArray.append(mindStoreController.view)
        
        for i in 0..<viewArray.count {
            let view = viewArray[i]
            view.frame = CGRect(x: CGFloat(i)*self.view.width, y: 0, width: scrollView.width, height: scrollView.height)
            scrollView.addSubview(view)
        }
        scrollView.contentSize = CGSize(width: CGFloat(viewArray.count)*scrollView.width, height: 0)
        
        scrollView.setContentOffset(CGPoint(x: scrollView.width, y: 0), animated: false)
    }
}

//MARK: --------------------------- Event and Action --------------------------
extension MainViewController {
    @objc private func menuBtnDidClick(clickView: UIView) {
        // 设置圆角
        UIView.animateWithDuration(0.1, animations: { [unowned self] in
            self.viewArray.forEach {
                let maskPath = UIBezierPath(roundedRect: $0.bounds, cornerRadius: self.vcState == .Small ?0:20)
                let maskLayer = CAShapeLayer()
                maskLayer.frame = $0.bounds
                maskLayer.path = maskPath.CGPath
                
                $0.layer.mask = maskLayer
            }
        })
        
        setupViewAnimation(clickView.tag)
    }
    
    
    /**
     执行放大收缩动画前需要隐藏和显示一些控件
     */
    private func setupViewHidden(tag: Int = 0) {
        
        // 设置状态栏
        statusBarHidden = vcState == .Small ?true:false
        setNeedsStatusBarAppearanceUpdate()
        // 设置菜单分类按钮,下拉刷新
        self.headerView.hidden = vcState == .Small ?false:true
        self.redLine.hidden = vcState == .Small ?false:true
    }
    
    /**
     这里是执行放大收缩的动画操作
     
     - parameter tag: 传入点击的view的tag
     */
    private func setupViewAnimation(tag: Int = 0) {
        // 缩放后子控件的宽
        let scaleWidth = vcState == .Small ?scrollView.width:scrollView.width*scale
        // scorllview中心点变化后的值
        let transY = vcState == .Small ?self.view.center.y+UIConstant.UI_MARGIN_10:scrollView.center.y+scrollView.height*(1-scale)*0.5
        let scrollViewTransCenter = CGPoint(x: self.scrollView.center.x, y: transY)
        // contentsize
        let contentSize = vcState == .Small ?CGSize(width: self.view.width*CGFloat(viewArray.count), height: 0):CGSize(width: (scaleWidth+UIConstant.UI_MARGIN_5)*CGFloat(self.viewArray.count), height: 0)
        // transform
        let scrollviewtransform = vcState == .Small ? CGAffineTransformIdentity:CGAffineTransformMakeScale(1, self.scale)
        let scrollsubviewTransform = vcState == .Small ?CGAffineTransformIdentity:CGAffineTransformMakeScale(self.scale, 1)
        // 获取点击后scrollview的contentoffset位置
        let index = vcState == .Small ?tag:Int(scrollView.contentOffset.x/self.view.width)
        var contentOffx = vcState == .Small ?CGFloat(index)*self.view.width:0.5*UIConstant.UI_MARGIN_5+CGFloat(index)*(scaleWidth+UIConstant.UI_MARGIN_5)
        contentOffx = contentOffx > contentSize.width-self.view.width ?contentSize.width-self.view.width:contentOffx
        // 设置头部空间位移差
        if vcState == .Small {
            let headerViewScale = self.view.width/(self.view.width*0.5-headerView.labelArray.last!.width*0.5)
            headerView.x = -contentOffx/headerViewScale
        }
        
        UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: { [unowned self] in
            // menubutton动画
            self.menuBtn.center = CGPoint(x: self.menuBtn.center.x, y: self.vcState == .Small ? 35 + self.menuBtn.frame.height/2 : -self.menuBtn.frame.height/2)
            self.menuBtn.alpha = self.vcState == .Small ? 1.0 : 0
            
            // scroollview动画
            self.scrollView.center = scrollViewTransCenter
            self.scrollView.transform = scrollviewtransform
            self.scrollView.contentSize = contentSize
            
            self.scrollView.setContentOffset(CGPoint(x: contentOffx,y: 0), animated: false)
            self.setupViewHidden(tag)
            // 设置srollview子控件的动画
            for i in 0..<self.viewArray.count {
                let view = self.viewArray[i]
                
                view.transform = scrollsubviewTransform
                let centerX = self.vcState == .Small ? scaleWidth*(CGFloat(i)+0.5):(scaleWidth+UIConstant.UI_MARGIN_5)*(CGFloat(i)+0.5)
                view.center = CGPoint(x: centerX, y: view.center.y)
            }
        }) {
            if $0 {
                
                // 添加按钮用于点击
                if self.vcState == .Small {
                    self.coverBtnArray.forEach {
                        $0.removeFromSuperview()
                    }
                } else {
                    for i in 0..<self.viewArray.count {
                        let view = self.viewArray[i]
                        
                        let coverBtn = self.createCoverBtn()
                        coverBtn.tag = i
                        coverBtn.frame = view.bounds
                        self.coverBtnArray.append(coverBtn)
                        view.addSubview(coverBtn)
                    }
                }
                // 设置scrollview
                self.scrollView.pagingEnabled = self.vcState == .Small ?true:false
                
                self.vcState = self.vcState == .Full ?.Small:.Full
            }
        }
    }
    
    /**
     创建一个按钮给Scrollview的5个控制器。便于点击之后恢复成填充状态  整个页面的控制器
     */
    private func createCoverBtn() -> UIButton {
        let coverBtn = UIButton()
        coverBtn.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.02)
        coverBtn.addTarget(self, action: #selector(MainViewController.menuBtnDidClick(_:)), forControlEvents: .TouchUpInside)
        return coverBtn
    }
}

// MARK: - 这里处理左右滑动时的一些动画（头部控件位移差，菜单按钮）
extension MainViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if vcState == .Full {
            // 设置头部空间位移差
            let scale = self.view.width/(self.view.width*0.5-headerView.labelArray.last!.width*0.5)
            headerView.x = -scrollView.contentOffset.x/scale
        }
    }
}

// MARK: - 这里传headerView给下拉刷新控件做处理
extension MainViewController: ScrollViewControllerReusableDataSource {
    
    func redLineView() -> UIView {
        return self.redLine
    }
    
    func menuButton() -> UIButton {
        return self.menuBtn
    }
    
    func titleHeaderView() -> MainHeaderView {
        return self.headerView
    }
}

//MARK: ------------ ScrollView上下滚动方向改变时调用，用于处理菜单按钮和分类按钮的动画 -----------
extension MainViewController: ScrollViewControllerReusableDelegate {
    func ScrollViewControllerDirectionDidChange(direction: ScrollViewDirection) {
        MenuBtnAnimation(direction)
    }
    /**
     菜单按钮动画
     */
    private func MenuBtnAnimation(dir: ScrollViewDirection) {
        // 位移
        let positionAnim = CABasicAnimation(keyPath: "position.y")
        positionAnim.fromValue = (dir == ScrollViewDirection.Down ?menuBtn.y:-menuBtn.height)
        
        positionAnim.toValue = (dir == ScrollViewDirection.Down ?(-menuBtn.height):menuBtn.y+20)
        
        // alpha
        let alphaAnim = CABasicAnimation(keyPath: "alpha")
        alphaAnim.fromValue = (dir == ScrollViewDirection.Down ?1:0)
        alphaAnim.toValue = (dir == ScrollViewDirection.Down ?0:1)
        
        let group = CAAnimationGroup()
        group.removedOnCompletion = false
        group.fillMode = kCAFillModeForwards
        group.animations = [positionAnim, alphaAnim]
        group.duration = 0.2
        
        menuBtn.layer.addAnimation(group, forKey: "hamburgButtonAnimation")
    }
}
